class UnitService {

	def cubeService

	def move = { unit, pathCubes ->
		if (pathCubes.size() > unit.type.move + 1) throw new IllegalArgumentException("${pathCubes} has more steps than the unit ${unit.type.name} can move ${unit.type.move}")
		def fromCube = pathCubes.remove(0)
		/*
		pathCubes.eachWithIndex { cube, i ->
			def unitCube = Cube.findByPlaceAndUnit(fromCube.place, unit)
			if (cubeService.isMoveOk(unitCube, cube)) {
				log.debug "move step ${unit} from ${unitCube} to ${cube}"
				unitCube = cube
			} else {
				throw new IllegalArgumentException("${cube} is not adjacent to ${unitCube} so unit ${unit} can't move to this cube !")
			}
		}
		*/
		
		fromCube.unit = null
		fromCube.save(flush:true)
		
		def toCube = pathCubes.last()
		toCube.unit = unit
		toCube.save()
	}
	
}